package cn.wolfcode.wolf2w.business.service.impl;

import cn.wolfcode.wolf2w.business.api.domain.StrategyComment;
import cn.wolfcode.wolf2w.business.mapper.StrategyCommentMapper;
import cn.wolfcode.wolf2w.business.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.business.service.IStrategyCommentService;
import cn.wolfcode.wolf2w.common.core.constant.SecurityConstants;
import cn.wolfcode.wolf2w.common.core.domain.R;
import cn.wolfcode.wolf2w.common.core.utils.StringUtils;
import cn.wolfcode.wolf2w.common.redis.service.RedisService;
import cn.wolfcode.wolf2w.member.api.RemoteUserInfoService;
import cn.wolfcode.wolf2w.member.api.domain.UserInfo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.jsonwebtoken.lang.Assert;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 攻略评论Service业务层处理
 * 
 * @author hyz
 * @date 2024-08-01
 */
@Service
@Transactional
public class StrategyCommentServiceImpl extends ServiceImpl<StrategyCommentMapper,StrategyComment> implements IStrategyCommentService {

    @Autowired
    private StrategyCommentMapper strategyCommentMapper;

    @Autowired
    private RemoteUserInfoService remoteUserInfoService;

    @Autowired
    private RedisService redisService;

//    @Override
//    public IPage<StrategyComment> queryPage(StrategyCommentQuery qo) {
//        IPage<StrategyComment> page = new Page<>(qo.getCurrentPage(), qo.getPageSize());
//        IPage<StrategyComment> pageResult = lambdaQuery().page(page);
//        for (StrategyComment comment : pageResult.getRecords()) {
//            R<UserInfo> r = remoteUserInfoService.getOne(comment.getUserId(), SecurityConstants.INNER);
//            if(r.getCode() == 200){
//                comment.setUser(r.getData());
//            }
//            if(!StringUtils.isBlank(comment.getThumbupids())){
//                String[] arr = comment.getThumbupids().split(",");
//                List<Long> list = comment.getThumbupidsList();
//                for (String s : arr) {
//                    list.add(Long.valueOf(s));
//                }
//            }
//        }
////        for (StrategyComment record : pageResult.getRecords()) {
////            System.err.println(record);
////        }
//
//        System.err.println(pageResult.getTotal());
//        return pageResult;
//    }

    @Override
    public IPage<StrategyComment> queryPage(StrategyCommentQuery qo) {
        IPage<StrategyComment> page = new Page<>(qo.getCurrentPage(), qo.getPageSize());
//        page.setOptimizeCountSql(false);
        IPage<StrategyComment> pageResult = lambdaQuery().eq(StrategyComment :: getStrategyId,qo.getStrategyId()).page(page);
        for (StrategyComment comment : pageResult.getRecords()){
            R<UserInfo> r = remoteUserInfoService.getOne(comment.getUserId(), SecurityConstants.INNER);
            if (r.getCode() == 200){
                comment.setUser(r.getData());
            }
            if(!StringUtils.isBlank(comment.getThumbupids())){
                String[] arr = comment.getThumbupids().split(",");
                List<Long> list = comment.getThumbupidsList();
                for (String s : arr) {
                    list.add(Long.valueOf(s));
                }
            }
        }
//        System.err.println(pageResult.getTotal());
        return pageResult;
    }


    @Override
    public void add(StrategyComment comment, Long uid) {
        String content = StringUtils.trimToNull(comment.getContent());
        Assert.notNull(content,"评论内容不能为空");
        comment.setUserId(uid);
        comment.setCreateTime(new Date());
        comment.setThumbnum(0l);
        comment.setState(1l);
        strategyCommentMapper.insert(comment);
    }
}
